package com.ruoyi.basedata.service.impl;

import com.ruoyi.basedata.domain.GoodUnit;
import com.ruoyi.basedata.mapper.GoodUnitMapper;
import com.ruoyi.basedata.mapper.GoodsMapper;
import com.ruoyi.basedata.service.IGoodUnitService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.system.api.domain.basedata.Goods;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品单位Service业务层处理
 *
 * @author lmj
 * @date 2024-10-24
 */
@Service
@Slf4j
@Transactional
public class GoodUnitServiceImpl implements IGoodUnitService {
    @Autowired
    private GoodUnitMapper goodUnitMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisService redisService;

    /**
     * 查询商品单位
     *
     * @param id 商品单位主键
     * @return 商品单位
     */
    @Override
    public GoodUnit selectGoodUnitById(Integer id) {
        return goodUnitMapper.selectGoodUnitById(id);
    }

    /**
     * 查询商品单位列表
     *
     * @param goodUnit 商品单位
     * @return 商品单位
     */
    @Override
    public List<GoodUnit> selectGoodUnitList(GoodUnit goodUnit) {
        return goodUnitMapper.selectGoodUnitList(goodUnit);
    }

    /**
     * 新增商品单位
     *
     * @param goodUnit 商品单位
     * @return 结果
     */
    @Override
    public AjaxResult insertGoodUnit(GoodUnit goodUnit) {
        log.info("GoodUnitServiceImpl insertGoodUnit param:{}", goodUnit);
        //判断单位名称是否重复
        int count = goodUnitMapper.getUnitCountByUnitName(goodUnit);
        if (count > 0) {
            return AjaxResult.warn("单位名称重复，请修改！");
        }
        boolean flag = goodUnitMapper.insertGoodUnit(goodUnit) > 0;
        if (flag) {
            return AjaxResult.success();
        } else {
            return AjaxResult.error("添加失败");
        }
//        return AjaxResult.success();
    }

    /**
     * 修改商品单位
     *
     * @param goodUnit 商品单位
     * @return 结果
     */
    @Override
    public AjaxResult updateGoodUnit(GoodUnit goodUnit) {
        log.info("FinerGoodUnitServiceImpl addUnit param:{}", goodUnit);
        //判断单位名称是否重复
        int count = goodUnitMapper.getUnitCountByUnitName(goodUnit);
        if (count > 0) {
            return AjaxResult.warn("单位名称重复，请修改！");
        }
        boolean flag = goodUnitMapper.insertGoodUnit(goodUnit) > 0;
        if (flag) {
            return AjaxResult.success("添加单位成功！");
        } else {
            return AjaxResult.error("添加单位失败！");
        }
    }

    /**
     * 批量删除商品单位
     *
     * @param ids 需要删除的商品单位主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteGoodUnitByIds(Integer[] ids) {
        //需要判断商品表中是否使用了该id对应的计量单位，有则不能删除
        Goods goods = new Goods();
        int delCount = 0;
        for (Integer id : ids) {
            goods.setId(id);
            int goodsCount = goodsMapper.selectGoodsList(goods).size();
            if (goodsCount > 0) {
                continue;
            }
            boolean flag = goodUnitMapper.deleteGoodUnitById(id) > 0;
            if (flag) {
                delCount++;
            } else {
                log.error("FinerGoodUnitServiceImpl deleteGoodUnitByIds error");
                throw new RuntimeException("删除单位错误");
            }
        }
        if (ids.length == delCount) {
            return AjaxResult.success("删除单位成功！");
        } else {
            return AjaxResult.warn("部分单位还有对应的商品在使用，不能删除！");
        }
    }

    /**
     * 删除商品单位信息
     *
     * @param id 商品单位主键
     * @return 结果
     */
    @Override
    public int deleteGoodUnitById(Integer id) {
        return goodUnitMapper.deleteGoodUnitById(id);
    }
}
